//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.app.service;

import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.base.domain.LiveBarrage;
import com.ruoyi.live.base.domain.LiveBarrageChannel;
import com.ruoyi.live.base.domain.LiveTaskUserChannel;
import com.ruoyi.live.base.mapper.LiveBarrageChannelMapper;
import com.ruoyi.live.base.mapper.LiveTaskUserChannelMapper;
import com.ruoyi.live.base.mapper.LiveTaskUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import jakarta.servlet.http.HttpServletRequest;

@Service
public class AppAsyncService {
    @Autowired
    VideoService videoService;
    @Autowired
    LiveTaskUserChannelMapper channelMapper;
    @Autowired
    CacheService cacheService;
    @Autowired
    LiveTaskUserMapper taskUserDao;
    @Autowired
    LiveTaskUserChannelMapper channelDao;
    @Autowired
    LiveBarrageChannelMapper lbcDao;

    public AppAsyncService() {
    }

    @Async
    public void doEndLive(HttpServletRequest request, Long id, Long examUserId) throws Exception {
        LiveTaskUserChannel channel = (LiveTaskUserChannel)this.channelMapper.selectById(id);
        if (channel == null) {
            throw new Exception("找不到这个视频");
        } else if (!channel.getCreateUser().equals(SessionInfo.getUserId())) {
            throw new Exception("不能关闭别人的任务");
        } else {
            if (channel.getState() == 1) {
                this.channelMapper.updateById(channel.setState(2).setEndTime(System.currentTimeMillis()));
                String filePath = request.getSession().getServletContext().getRealPath("");
                this.videoService.getLiveVideoInfo(id, filePath, channel.getServerId(), channel.getChannelName());
            }

        }
    }

    @Async
    public void saveQuestionAsync(LiveBarrage barrage, Long taskUserId, Long channelId, Integer timeIndex) {
        this.taskUserDao.updateCount(taskUserId);
        LiveTaskUserChannel channel = (LiveTaskUserChannel)this.channelDao.selectById(channelId);
        if (channel != null) {
            LiveBarrageChannel lbc = (new LiveBarrageChannel()).setChannelId(channelId).setType(5).setBarrageId(barrage.getId()).setTimeIndex(timeIndex).setFromUserName(barrage.getUserName()).setFromUserLevel(barrage.getLevel()).setUserAddCount(barrage.getAddCount()).setCreateTime(System.currentTimeMillis());
            this.lbcDao.insert(lbc);
        }

    }
}
